const config = require('./config.js')


// promise finally polyfill
Promise.prototype.finally = function(callback) {
    let P = this.constructor
    return this.then(
        value => P.resolve(callback()).then(() => value),
        reason => P.resolve(callback()).then(() => { throw reason })
    )
}

/**
 * 等待信号再执行下一步
 * @param {信号源，一般用全局对象} signalOrigin
 * @param {信号字段} flag
 * @param {触发状态，默认false} status
 */
function waitFlag(signalOrigin, flag, status) {
    return new Promise(function(resolve, reject) {
        let i = 0
        let check = function() {
            if (!!signalOrigin[flag] === !!status) {
                resolve()
            } else if (i++ > 1000) {
                reject()
            } else {
                setTimeout(check, 50)
            }
        }
        check()
    })
}


module.exports = {waitFlag}